#!/usr/bin/env python
import argparse

def str_to_hex(line):
    result = 0
    bitNum = len(line)
    for i in range(bitNum):
        if ( line[i] == '1'):
            result |= 1 << (bitNum - i - 1)
    return result

def font_code_list_conver(LineList,isArrEnd):
    result = 0
    for i in range(len(LineList)):
        result = str_to_hex(LineList[i])
        if ( isArrEnd and i == len(LineList) -1 ):
            print("0x%02x"%(result),end="")
        else:
            print("0x%02x,"%(result),end="")

    print("\n",end="")
    if ( isArrEnd ):
        print("};",end="")

def parse_font_code_line(line):
    isArrEnd = 0
    if '}' in line:
        isArrEnd= 1
    line = line.strip("\n").strip().strip(",").strip(";").strip("}").lower()
    line = line.replace("_","0").replace("x","1").split(',')
    font_code_list_conver(line,isArrEnd)

def is_font_code_line(line):
    line = line.strip("\n").strip().strip(";").strip("}").lower()
    line = line.replace("_","0").replace("x","1").replace(",","")
    if  line.isnumeric() :
        return True
    else:
        return False

def parse_whole_file(srcFileName):
    srcFile = open(srcFileName,"r")
    srcFileList = srcFile.readlines()
    for line in srcFileList:
        if ( is_font_code_line(line)):
            parse_font_code_line(line)
        else:
            print(line,end="")


def main():
    parser = argparse.ArgumentParser(description='font code conversion.')
    parser.add_argument('src')
    args = parser.parse_args()
    parse_whole_file(args.src)
    
main()
